.PHONY: help install dev test build deploy clean logs shell

# 默认目标
help:
	@echo "📚 在线智能考试系统 - 可用命令:"
	@echo ""
	@echo "🚀 开发命令:"
	@echo "  install     - 安装所有依赖"
	@echo "  dev         - 启动开发环境"
	@echo "  down        - 停止开发环境"
	@echo "  logs        - 查看服务日志"
	@echo "  shell       - 进入后端shell"
	@echo ""
	@echo "🧪 测试命令:"
	@echo "  test        - 运行所有测试"
	@echo "  test-backend - 运行后端测试"
	@echo "  test-frontend- 运行前端测试"
	@echo "  test-ai      - 运行AI服务测试"
	@echo ""
	@echo "🔧 管理命令:"
	@echo "  migrate     - 运行数据库迁移"
	@echo "  createsuperuser - 创建超级用户"
	@echo "  collectstatic - 收集静态文件"
	@echo "  loaddata    - 加载初始数据"
	@echo ""
	@echo "🏗️ 构建命令:"
	@echo "  build       - 构建Docker镜像"
	@echo "  rebuild     - 重新构建镜像"
	@echo "  deploy      - 部署到生产环境"
	@echo ""
	@echo "🧹 清理命令:"
	@echo "  clean       - 清理临时文件"
	@echo "  clean-all   - 清理所有数据"

# 安装依赖
install:
	@echo "📦 安装项目依赖..."
	@cp .env.example .env
	@echo "✅ 请编辑 .env 文件配置您的环境变量"
	@echo "✅ 依赖安装完成"

# 启动开发环境
dev:
	@echo "🚀 启动开发环境..."
	docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
	@echo "✅ 开发环境已启动"
	@echo "🌐 前端地址: http://localhost:3000"
	@echo "🔧 后端API: http://localhost:8000/api/v1"
	@echo "🤖 AI服务: http://localhost:5000/api/v1"
	@echo "🌸 Celery监控: http://localhost:5555"

# 停止开发环境
down:
	@echo "🛑 停止开发环境..."
	docker-compose down
	@echo "✅ 开发环境已停止"

# 查看日志
logs:
	@echo "📋 查看服务日志..."
	docker-compose logs -f

# 查看特定服务日志
logs-backend:
	docker-compose logs -f backend

logs-frontend:
	docker-compose logs -f frontend

logs-ai:
	docker-compose logs -f ai-service

# 运行测试
test:
	@echo "🧪 运行所有测试..."
	@$(MAKE) test-backend
	@$(MAKE) test-frontend
	@$(MAKE) test-ai

test-backend:
	@echo "🐍 运行后端测试..."
	docker-compose exec backend python manage.py test

test-frontend:
	@echo "⚛️ 运行前端测试..."
	docker-compose exec frontend npm run test:unit

test-ai:
	@echo "🤖 运行AI服务测试..."
	docker-compose exec ai-service python -m pytest

# 数据库迁移
migrate:
	@echo "🔄 运行数据库迁移..."
	docker-compose exec backend python manage.py makemigrations
	docker-compose exec backend python manage.py migrate
	@echo "✅ 数据库迁移完成"

# 创建超级用户
createsuperuser:
	@echo "👤 创建超级用户..."
	docker-compose exec backend python manage.py createsuperuser

# 收集静态文件
collectstatic:
	@echo "📁 收集静态文件..."
	docker-compose exec backend python manage.py collectstatic --noinput
	@echo "✅ 静态文件收集完成"

# 加载初始数据
loaddata:
	@echo "📊 加载初始数据..."
	docker-compose exec backend python manage.py loaddata fixtures/initial_data.json
	@echo "✅ 初始数据加载完成"

# 构建镜像
build:
	@echo "🏗️ 构建Docker镜像..."
	docker-compose build
	@echo "✅ 镜像构建完成"

# 重新构建镜像
rebuild:
	@echo "🔄 重新构建Docker镜像..."
	docker-compose build --no-cache
	@echo "✅ 镜像重新构建完成"

# 部署到生产环境
deploy:
	@echo "🚀 部署到生产环境..."
	docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
	@$(MAKE) migrate
	@$(MAKE) collectstatic
	@echo "✅ 部署完成"

# 清理临时文件
clean:
	@echo "🧹 清理临时文件..."
	docker-compose down -v
	docker system prune -f
	cd frontend && rm -rf node_modules dist
	cd backend && find . -type d -name __pycache__ -delete
	cd ai-service && find . -type d -name __pycache__ -delete
	@echo "✅ 清理完成"

# 清理所有数据（谨慎使用）
clean-all:
	@echo "⚠️ 清理所有数据（包括数据库）..."
	@read -p "确定要继续吗？[y/N] " confirm && [ "$$confirm" = "y" ]
	docker-compose down -v --remove-orphans
	docker system prune -af --volumes
	docker volume prune -f
	@echo "✅ 清理完成"

# 进入后端shell
shell:
	@echo "🐚 进入后端shell..."
	docker-compose exec backend python manage.py shell

# 进入数据库
db:
	@echo "🗄️ 进入数据库..."
	docker-compose exec mysql mysql -u root -p

# 备份数据库
backup:
	@echo "💾 备份数据库..."
	mkdir -p backups
	docker-compose exec mysql mysqldump -u root -p soes_db > backups/backup_$$(date +%Y%m%d_%H%M%S).sql
	@echo "✅ 数据库备份完成"

# 恢复数据库
restore:
	@echo "📥 恢复数据库..."
	@read -p "请输入备份文件路径: " backup_file
	docker-compose exec -T mysql mysql -u root -p soes_db < $$backup_file
	@echo "✅ 数据库恢复完成"

# 重启服务
restart:
	@echo "🔄 重启服务..."
	docker-compose restart
	@echo "✅ 服务重启完成"

# 查看服务状态
status:
	@echo "📊 查看服务状态..."
	docker-compose ps

# 更新依赖
update:
	@echo "🔄 更新依赖..."
	cd backend && pip install -r requirements/production.txt --upgrade
	cd frontend && npm update
	cd ai-service && pip install -r requirements/production.txt --upgrade
	@echo "✅ 依赖更新完成"
